package cl.loso.melon.server.persistencia;

import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cl.loso.melon.server.gae.PMF;
import cl.loso.melon.server.model.CargoLVS;


public class CargoLVSDAO {
	private static Log log = LogFactory.getLog(CargoLVSDAO.class);

	public static void insertar(CargoLVS cargo) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	pm.makePersistent(cargo);
	    } catch (Exception e) {
	    	log.error(e.getMessage());
	    } finally {
	        pm.close();
	    }			
	}
	
	@SuppressWarnings("unchecked")
	public static List<CargoLVS> buscarNombre(String pnombre) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    List<CargoLVS> detached = null;

	    try {
	    	Query query = pm.newQuery(CargoLVS.class,"nombre == pnombre");
	    	query.declareParameters("String pnombre");

	    	detached = (List<CargoLVS>)pm.detachCopyAll((List<CargoLVS>) query.execute(pnombre));
  
	    } catch (Exception e) {
	    	log.error(e.getMessage());	        
	    } finally {
	        pm.close();
	    }
	    return detached;		

	}
	public static void borrar(Long id) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	pm.deletePersistent(pm.getObjectById(CargoLVS.class, id));
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }		
		
	}
	
	public static void actualizar(Long id,String nombre) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	CargoLVS cargo=pm.getObjectById(CargoLVS.class, id);
	    	cargo.setNombre(nombre);
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }		

	}	


	@SuppressWarnings("unchecked")
	public static List<CargoLVS> obtener() {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    List<CargoLVS> cargoLst=null;
	    Query query=null;
	    try {
	    	query = pm.newQuery(CargoLVS.class);
	    	query.setOrdering("nombre ASC");
	    	cargoLst = (List<CargoLVS>)pm.detachCopyAll((List<CargoLVS>)query.execute());
	    } catch (Exception e) {
	    	log.error(e.getMessage());	    	
	    } finally {
	        pm.close();
	    }
	    return cargoLst; 
	}

	public static CargoLVS getCargobyId(Long id) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    CargoLVS cargo, detached = null;
	    try {
	    	cargo = pm.getObjectById(CargoLVS.class, id);
	        detached = pm.detachCopy(cargo);
	    } catch (Exception e) {
	    	log.error(e.getMessage());	        
	    } finally {
	        pm.close();
	    }
	    return detached;		

	}
}
